(function (window, document) {
    var Meeting = function (id, type, time, ary) {

        // 二次渲染的区块ID
        var tid = id;

        // 重置清除
        $(tid).find("tr td").removeClass("sel");
        $(tid).find("tr td").unbind("hover");

        // 获取后台数据数组
        var list = ary.meeting;

        // 日期
        var day = "";

        // 搜索条件：会议室
        if (type == 0) {
            $(tid).find("tr").show();
        } else {
            $(tid).find("tr").hide();
            $(tid).find("tr:nth-child(" + type + ")").show();
        }

        // 搜索条件：时间
        if (time == null || time == undefined || time == "") {
            var timestamp = Date.parse(new Date());
            day = String((timeFormat(timestamp)));
        } else {
            day = time;
        };

        // 循环判断
        for (var i = 0; i < list.length; i++) {

            // 截取时间保留年月日
            var startDay = String(list[i].meet.startTime.substring(0, 10));
            var endDay = String(list[i].meet.endTime.substring(0, 10));

            // 获取开始结束时间范围内的所有日期
            var seDay = [];
            seDay = getdiffdate(startDay, endDay);

            // 检索时间条件是否在时间区间内
            var inDay = $.inArray(day, seDay);

            // 截取时间保留小时分钟
            var startTime = String(list[i].meet.startTime.substring(11, 16));
            var endTime = String(list[i].meet.endTime.substring(11, 16));

            // 时间范围-时间格子
            var seTime = ["08:30", "09:00", "09:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", ]

            // 时间条件等于当天，并且范围在当天内
            if (inDay == 0 && seDay.length == 1) {

                var sTd = $.inArray(startTime, seTime) + 2;
                var eTd = $.inArray(endTime, seTime) + 2;
                if (list[i].meetType == 1) {
                    for (var j = sTd; j < eTd; j++) {
                        $(tid).find("tr:first-child td:nth-child(" + j + ")").addClass("sel");
                        $(tid).find("tr:first-child td:nth-child(" + j + ")").bind("hover", function (event) {
                            if (event.type == 'mouseenter') {
                                var index = $(this).parents("tr").find("td").index($(this));
                                var trNo = $(this).parents("tr").index();
                                var id = $(this).attr("id", trNo + "-" + index);
                                seltips(id, day, index, trNo, list);
                            } else {
                                layer.closeAll('tips');
                            }
                        });
                    }
                } else {
                    var trNo = list[i].meetType;
                    for (var j = sTd; j < eTd; j++) {
                        $(tid).find("tr:nth-child(" + trNo + ") td:nth-child(" + j + ")").addClass("sel");
                        $(tid).find("tr:nth-child(" + trNo + ") td:nth-child(" + j + ")").bind("hover", function (event) {
                            if (event.type == 'mouseenter') {
                                var index = $(this).parents("tr").find("td").index($(this));
                                var trNo = $(this).parents("tr").index();
                                var id = $(this).attr("id", trNo + "-" + index);
                                seltips(id, day, index, trNo, list);
                            } else {
                                layer.closeAll('tips');
                            }
                        });
                    }
                }
            }
            // 时间条件等于当天，并且开始时间在当天内
            if (inDay == 0 && seDay.length > 1 && inDay != seDay.length - 1) {
                var sTd = $.inArray(startTime, seTime) + 2;
                var eTd = seTime.length + 1;

                if (list[i].meetType == 1) {
                    for (var j = sTd; j < eTd; j++) {
                        $(tid).find("tr:first-child td:nth-child(" + j + ")").addClass("sel");
                        $(tid).find("tr:first-child td:nth-child(" + j + ")").bind("hover", function (event) {
                            if (event.type == 'mouseenter') {
                                var index = $(this).parents("tr").find("td").index($(this));
                                var trNo = $(this).parents("tr").index();
                                var id = $(this).attr("id", trNo + "-" + index);
                                seltips(id, day, index, trNo, list);
                            } else {
                                layer.closeAll('tips');
                            }
                        });
                    }
                } else {
                    var trNo = list[i].meetType;
                    for (var j = sTd; j < eTd; j++) {
                        $(tid).find("tr:nth-child(" + trNo + ") td:nth-child(" + j + ")").addClass("sel");
                        $(tid).find("tr:nth-child(" + trNo + ") td:nth-child(" + j + ")").bind("hover", function (event) {

                            if (event.type == 'mouseenter') {
                                var index = $(this).parents("tr").find("td").index($(this));
                                var trNo = $(this).parents("tr").index();
                                var id = $(this).attr("id", trNo + "-" + index);
                                seltips(id, day, index, trNo, list);
                            } else {
                                layer.closeAll('tips');
                            }
                        });
                    }
                }
            }
            // 时间条件等于当天，并且结束时间在当天内
            if (seDay.length > 1 && inDay == seDay.length - 1) {
                var sTd = 2;
                var eTd = $.inArray(endTime, seTime) + 2;

                if (list[i].meetType == 1) {
                    for (var j = sTd; j < eTd; j++) {
                        $(tid).find("tr:first-child td:nth-child(" + j + ")").addClass("sel");
                        $(tid).find("tr:first-child td:nth-child(" + j + ")").bind("hover", function (event) {
                            if (event.type == 'mouseenter') {
                                var index = $(this).parents("tr").find("td").index($(this));
                                var trNo = $(this).parents("tr").index();
                                var id = $(this).attr("id", trNo + "-" + index);
                                seltips(id, day, index, trNo, list);
                            } else {
                                layer.closeAll('tips');
                            }
                        });
                    }
                } else {
                    var trNo = list[i].meetType;
                    for (var j = sTd; j < eTd; j++) {
                        $(tid).find("tr:nth-child(" + trNo + ") td:nth-child(" + j + ")").addClass("sel");
                        $(tid).find("tr:nth-child(" + trNo + ") td:nth-child(" + j + ")").bind("hover", function (event) {
                            if (event.type == 'mouseenter') {
                                var index = $(this).parents("tr").find("td").index($(this));
                                var trNo = $(this).parents("tr").index();
                                var id = $(this).attr("id", trNo + "-" + index);
                                seltips(id, day, index, trNo, list);
                            } else {
                                layer.closeAll('tips');
                            }
                        });
                    }
                }

            }
            // 时间条件等于当天，开始时间or结束时间都不在当天内
            if (seDay.length > 1 && inDay > 0 && inDay < seDay.length - 1) {
                var sTd = 2;
                var eTd = seTime.length + 1;

                if (list[i].meetType == 1) {
                    for (var j = sTd; j < eTd; j++) {
                        $(tid).find("tr:first-child td:nth-child(" + j + ")").addClass("sel");
                        $(tid).find("tr:first-child td:nth-child(" + j + ")").bind("hover", function (event) {
                            if (event.type == 'mouseenter') {
                                var index = $(this).parents("tr").find("td").index($(this));
                                var trNo = $(this).parents("tr").index();
                                var id = $(this).attr("id", trNo + "-" + index);
                                seltips(id, day, index, trNo, list);
                            } else {
                                layer.closeAll('tips');
                            }
                        });
                    }
                } else {
                    var trNo = list[i].meetType;
                    for (var j = sTd; j < eTd; j++) {
                        $(tid).find("tr:nth-child(" + trNo + ") td:nth-child(" + j + ")").addClass("sel");
                        $(tid).find("tr:nth-child(" + trNo + ") td:nth-child(" + j + ")").bind("hover", function (event) {
                            if (event.type == 'mouseenter') {
                                var index = $(this).parents("tr").find("td").index($(this));
                                var trNo = $(this).parents("tr").index();
                                var id = $(this).attr("id", trNo + "-" + index);
                                seltips(id, day, index, trNo, list);
                            } else {
                                layer.closeAll('tips');
                            }
                        });
                    }
                }
            }

            // 时间条件不等于当天，开始时间or结束时间都不在条件内
            // if (inDay < 0) {
            //     console.log(list[i].meetType);
            //     if (list[i].meetType == 1) {
            //         $(tid).find("tr:first-child td").removeClass("sel");
            //         $(tid).find("tr:first-child td").unbind("hover");
            //     } else {
            //         var trNo = list[i].meetType;
            //         $(tid).find("tr:nth-child(" + trNo + ") td").removeClass("sel");
            //         $(tid).find("tr:nth-child(" + trNo + ") td").unbind("hover");
            //     }
            // }

        }
    };
    Meeting.prototype = {};

    window.Meeting = Meeting;
}(window, document));


//时间戳转化成时间格式
function add0(m) {
    return m < 10 ? '0' + m : m
};

function timeFormat(timestamp) {
    //timestamp是整数，否则要parseInt转换,不会出现少个0的情况
    var time = new Date(timestamp);
    var year = time.getFullYear();
    var month = time.getMonth() + 1;
    var date = time.getDate();
    // var hours = time.getHours();
    // var minutes = time.getMinutes();
    // var seconds = time.getSeconds();
    // return year + '-' + add0(month) + '-' + add0(date) + ' ' + add0(hours) + ':' + add0(minutes) + ':' + add0(seconds);
    return year + '-' + add0(month) + '-' + add0(date);
};


//获取两日期之间日期列表函数
function getdiffdate(stime, etime) {
    //初始化日期列表，数组
    var diffdate = new Array();
    var i = 0;
    //开始日期小于等于结束日期,并循环
    while (stime <= etime) {
        diffdate[i] = stime;

        //获取开始日期时间戳
        var stime_ts = new Date(stime).getTime();
        // console.log('当前日期：' + stime + '当前时间戳：' + stime_ts);

        //增加一天时间戳后的日期
        var next_date = stime_ts + (24 * 60 * 60 * 1000);

        //拼接年月日，这里的月份会返回（0-11），所以要+1
        var next_dates_y = new Date(next_date).getFullYear() + '-';
        var next_dates_m = (new Date(next_date).getMonth() + 1 < 10) ? '0' + (new Date(next_date).getMonth() + 1) + '-' : (new Date(next_date).getMonth() + 1) + '-';
        var next_dates_d = (new Date(next_date).getDate() < 10) ? '0' + new Date(next_date).getDate() : new Date(next_date).getDate();

        stime = next_dates_y + next_dates_m + next_dates_d;

        //增加数组key
        i++;
    }
    return diffdate;
}

//经过效果，根据条件：日期、时间、会议室、原始数据集合
function seltips(id, day, time, type, list) {

    var seTime = ["08:30", "09:00", "09:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00"]
    time = seTime[time - 1];

    seTime = day + " " + time;

    type = type + 1;

    var date = new Date(seTime.replace(/-/g, '/'));

    for (var i in list) {

        // console.log(list[i].meetType);

        if (list[i].meetType === type) {
            
            var stime = new Date(list[i].meet.startTime.replace(/-/g, '/'));
            var etime = new Date(list[i].meet.endTime.replace(/-/g, '/'));

            if (date >= stime && date <= etime) {

                var html = '<ul>' +
                    '<li>预订人:' + list[i].users.userName + '</li>' +
                    '<li>预定部门:' + list[i].users.dep + '</li>' +
                    '<li>会议室编号:' + list[i].meet.meetNo + '</li>' +
                    '<li>会议室名称:' + list[i].meet.meetName + '</li>' +
                    '<li>会议主题:' + list[i].meet.meetTitle + '</li>' +
                    '<li>会议开始时间:' + list[i].meet.startTime + '</li>' +
                    '<li>会议结束时间:' + list[i].meet.endTime + '</li>' +
                    '<li>预订人电话:' + list[i].users.meetTell + '</li>' +
                    '<li>预订人邮箱:' + list[i].users.meetEmail + '</li>' +
                    '</ul>'
                var index = layer.tips(html, id, {
                    tips: [3, '#3595CC']
                });
                return index;
            }
        }
    }
}